package com.songlanyun.modules.refillorder.controller.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.utils.OrderGen;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.rabbitmq.mq.producer.SendOrderOrRightsOrderMsgService;
import com.songlanyun.modules.refillorder.model.RefillCardOrderDTO;
import com.songlanyun.modules.refillorder.model.RefillOrderPayVo;
import com.songlanyun.pay.bean.PayBean;
import com.songlanyun.pay.common.api.ApiResult;
import com.songlanyun.pay.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.refillorder.entity.SaleRefillCardOrderEntity;
import com.songlanyun.modules.refillorder.service.SaleRefillCardOrderService;
import com.songlanyun.common.utils.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 充值卡订单
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-16 14:26:32
 */
@RestController
@RequestMapping("api/order/refillcard")
@Slf4j
public class SaleRefillCardOrderApiController extends AbstractApiController {

    @Resource
    private SaleRefillCardOrderService saleRefillCardOrderService;

    @Resource
    private SendOrderOrRightsOrderMsgService sendOrderOrRightsOrderMsgService;

    @Resource
    @Qualifier("payService2")
    private PayService payService;

    /**
     * 会员查询自己的充值卡订单
     */
    @GetMapping("/list")
    public R list() {
        List<SaleRefillCardOrderEntity> list = saleRefillCardOrderService.list(
                new LambdaQueryWrapper<SaleRefillCardOrderEntity>()
                        .eq(SaleRefillCardOrderEntity::getUserId, getUserId(false))
                        .orderByDesc(SaleRefillCardOrderEntity::getCreateTimeMillis)
        );

        return R.ok().put("list", list);
    }


    /**
     * 详情
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SaleRefillCardOrderEntity saleRefillCardOrder = saleRefillCardOrderService.getById(id);

        return R.ok().put("data", saleRefillCardOrder);
    }

    /**
     * 会员购买充值卡
     */
    @RepeatSubmit(value = "3")
    @PostMapping("/buy")
    public R buy(@RequestBody RefillCardOrderDTO refillCardOrderDTO) {
        //设置会员id
        refillCardOrderDTO.setUserId(getUserId(true));

        ValidatorUtils.validateEntity(refillCardOrderDTO);

        refillCardOrderDTO.setClient(getClient());
        //购买
        RefillOrderPayVo buy = saleRefillCardOrderService.buy(refillCardOrderDTO);

        //下单成功发送消息
        sendOrderOrRightsOrderMsgService.createPayOrderSuccessSendMsg(buy.getPayOrder());

        return R.ok().put("data", buy.getApiResult());

    }

    /**
     * 会员直接充值
     */
    @RepeatSubmit(value = "3")
    @PostMapping("/directBuy")
    public R directBuy(@RequestBody RefillCardOrderDTO refillCardOrderDTO) {


        throw new RuntimeException("该功能暂时关闭");
//        //设置会员id
//        refillCardOrderDTO.setUserId(getUserId(true));
//
//        refillCardOrderDTO.setClient(getClient());
//        //购买
//        RefillOrderPayVo buy = saleRefillCardOrderService.directBuy(refillCardOrderDTO);
//
//        //下单成功发送消息
//        sendOrderOrRightsOrderMsgService.createPayOrderSuccessSendMsg(buy.getPayOrder());
//
//        return R.ok().put("data", buy.getApiResult());

    }

}
